<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class DetailTemplate {

    private $mRawData; //Mentahan query data
    private $mData;
    private $mDetailTitle;
    private $mFieldId = array();
    private $mFieldHidden = array();
    private $mFieldUCWords = array();
    private $mFieldFormatBold = array();
    private $mFieldClass = array();

    public function __construct($pRawData) {
        if (is_object($pRawData)) {
            $tFungsi= new Fungsi();
            $tFungsi->CalibrateArray($pRawData);
            
            $this->mRawData = $pRawData;
            $this->mRawData->CalibrateField();
            $this->mData = $this->mRawData->ParseSQL();
        } else {
            //ERROR: Bukan merupakan object       
        }
    }

    public function SetFieldId($pId, $pIndex) {
        $this->mFieldId[$pIndex] = $pId;
    }
    
    public function SetFieldHidden($pIndex) {
        $this->mFieldHidden[$pIndex] = true;
    }

    public function SetFieldAddClass($pClass, $pIndex) {
        $this->mFieldClass[$pIndex] = $pClass;
    }

    public function SetFieldUCWords($pIndex) {
        $this->mFieldUCWords[$pIndex] = true;
    }

    public function SetFieldFormatBold($pIndex) {
        $this->mFieldFormatBold[$pIndex] = true;
    }

    public function GetData($pIndexValue) {
        if ($this->mData->MoveFirst()) {
            //if (is_string($pIndexValue)) {
            $tRes = $this->mData->$pIndexValue;
            //}
        }
        return $tRes;
    }

    public function PrintParsedDataToDetail() {
        try {
            if ($this->mData->MoveFirst()) {
                $tTempField = $this->mRawData->GetField();

                if (is_array($tTempField) && !empty($tTempField)) {
                    $no = 1; $tCountHidden=0;
                    foreach ($tTempField as $tField) {
                        //GET ONLY ALIAS (nama tabel/alias tabel tidak disertakan)
                        $tFieldDB = strtolower($tField[FieldDB]);
                        //CHECK APAKAH SUB QUERY
                        $tPos = strpos($tFieldDB, " as ");
                        if ($tPos) {
                            $tFieldDB = substr($tFieldDB, ($tPos + 4));
                        } else {
                            unset($tPos);
                            $tPos = strpos($tFieldDB, ".");
                            if ($tPos)
                                $tFieldDB = substr($tFieldDB, ($tPos + 1));
                        }
                        //CHECK: Is Field Hidden?
                        if (!isset($this->mFieldHidden[$no - 1])) {
                            $tContent.= "<tr " . $this->SetRowColor($no-$tCountHidden) . " >\n";
                            $tContent.= "\t<td class=\"Caption\">" . $tField[FieldName] . "</td>\n";
                            $tContent.= "\t<td class=\"Separator\"> : </td>\n";

                            //SETUP FORMAT
                            //TEXT-BOLD
                            if ($this->mFieldFormatBold[$no - 1])
                                $tData = "<b>" . $this->mData->$tFieldDB . "</b>";
                            else
                                $tData = $this->mData->$tFieldDB;


                            if ($this->mFieldUCWords[$no - 1])
                                $tContent.= "\t<td ".($this->mFieldId[$no - 1]?"id=\"".$this->mFieldId[$no - 1]."\"" : "")." class=\"Value " . ($this->mFieldClass[$no - 1] ? $this->mFieldClass[$no - 1] : "") . "\">" . ucwords(strtolower($tData)) . "</td>\n";
                            else
                                $tContent.= "\t<td ".($this->mFieldId[$no - 1]?"id=\"".$this->mFieldId[$no - 1]."\"" : "")." class=\"Value " . ($this->mFieldClass[$no - 1] ? $this->mFieldClass[$no - 1] : "") . "\">" . $tData . "</td>\n";
                            $tContent.= "</tr>";
                        }
                        else {
                            $tCountHidden++;
                        }
                        $no++;
                    }
                }
            }
        } catch (Exception $ex) {
            $tContent = "<tr><td colspan=\"" . ($tJmlCol - count($this->mFieldHidden) ) . "\">\n" .
                    "Exception Caught: " . $ex->getMessage() . "\n" .
                    "<p>Trace: " . $ex->getTraceAsString() . "</p> \n" .
                    "</td></tr>";
        }

        return $tContent;
    }

    private function SetRowColor($pRowIndex) {
        //FUNGSI untuk menset warna zebra background pada list
        $tTemp = $pRowIndex / 2;
        if (substr($tTemp, -2, 1) == '.')
            $color = TEMPLATE_SETTING_LIST_ODD_BG;
        else
            $color = TEMPLATE_SETTING_LIST_EVEN_BG;

        return "bgcolor=\"" . $color . "\"";
    }

    public function SetTitle($pTitle) {
        $this->mDetailTitle = $pTitle;
    }

    protected function PrintTitle() {
        if (!empty($this->mDetailTitle)) {
            $tOutput = "<div class=\"Template-Detail\">";
            $tOutput.= "<h1>$this->mDetailTitle</h1>";
            $tOutput.= "</div>";
        }
        return $tOutput;
    }

    public function PrintDetail() {
        echo '<div class="Template-Detail">';
        echo $this->PrintTitle();
        echo "<table class=\"Template-Detail\" cellspacing=0>";
        echo $this->PrintParsedDataToDetail();
        echo "</table>";
        echo '</div>';
    }

}

?>
